package com.kzw.web;

import com.kzw.entity.User;
import com.kzw.service.UserService;

import my.mvc.RequestContext;
import my.mvc.Annotation.JSONOutput;
import my.vo.Record;

public class UserAction {

	private UserService userService = new UserService();
	
	// AJAX请求
	@JSONOutput
	public Record login(RequestContext rc) {
		String uname = rc.param("uname");
		String passwd = rc.param("passwd");
		
		User user = userService.valid(uname, passwd);
		if(user == null) {
			return new Record(false, 1, "账号或密码错误");
		}
		
		// 登陆成功，将用户信息放入session
		rc.session().setAttribute("USER", user);
		
		// ajax请求，返回JSON，没有页面跳转
		return new Record(true);
	}
	
	// 普通请求，页面跳转
	public void logout(RequestContext rc) throws Exception {
		rc.session().invalidate();
		// 重定向
		rc.redirect(rc.contextPath() + "/login.jsp");
	}
}
